package com.liliya.dao.impl;

import com.liliya.dao.BaseDao;
import com.liliya.dao.EmpDao;
import com.liliya.pojo.Emp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/1/5 11:25
 */
public class EmpDaoImpl extends BaseDao implements EmpDao {
    @Override
    public List<Emp> findEmps(Integer start, Integer end) {
        List<Emp> list = new ArrayList<Emp>();
        try {
            String sql = "select empno,ename from (select empno,ename,rownum r from emp ) e "
                    + "where e.r between ? and ?";
            conn = getConn();
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, start);
            pstm.setInt(2, end);
            rs = pstm.executeQuery();
            while (rs.next()) {
                Emp emp = new Emp(rs.getInt("empno"), rs.getString("ename"));
                list.add(emp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //记得这里要关闭
        close();
        return list;
    }

    @Override
    public Integer findEmpCount() {
        int count = 0;
        String sql = "select count(1) c from emp";
        try {
            conn = getConn();
            pstm =  conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()){
                count = rs.getInt("c");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        close();
        return count;
    }
}
